
from wtforms import form, fields, validators

from hames.ft import FunctionalTest

class DomainTestFT(FunctionalTest):
    pass

REQUIRED = validators.required()
OPTIONAL = validators.optional()

class CreateAccountFT(FunctionalTest):
    """
    An example functional test for creating an account in the datastore.
    """
    NAME = 'Create Test Account'
    CATEGORY = 'Accounts'
    DESCRIPTION = 'Used to create test accounts, populating all the necesary fields and creating sub-entities.'

    def process(self, form):
        """
        Get an instance of the form, process it.
        """
        # the form will be valid by this point
        from my.domain import create_account
        self.appendMessage('Creating account.')
        account = create_account(**form.data)
        self.appendMessage('Created account %s' % account.account_id)

    class AccountForm(form.Form):
        """ Form for searching for assets. """
        email      = fields.TextField(label='Email', default='joe@example.com', validators=[REQUIRED])
        first_name = fields.TextField(label='First name', default='Joe', validators=[OPTIONAL])
        last_name  = fields.TextField(label='Last name', default='Smith', validators=[OPTIONAL])
        phone      = fields.TextField(label='Phone', default='1-306-555-1212', validators=[OPTIONAL])

        def validate_first_name(form, field):
            """ Called by the wtform framework; the name of the method is the key. """
            if (not field.data and form.last_name.data) or (field.data and not form.last_name.data):
                raise ValueError('Both first name and last name must be specified if either are.')

    FORM_CLASS = AccountForm
